package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.pojo.SpecialClinic;
import cn.edu.neusoft.demo.hospital2.Service.SpecialClinicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/clinic")
public class SpecialClinicController {

    @Autowired
    private SpecialClinicService specialClinicService;

    @GetMapping("/all")
    public Map<String, Object> getAllSpecialClinics() {
        Map<String, Object> result = new HashMap<>();
        List<SpecialClinic> list = specialClinicService.getAllSpecialClinics();
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/byDepartmentId")
    public Map<String, Object> getSpecialClinicsByDepartmentId(@RequestParam Integer departmentId) {
        Map<String, Object> result = new HashMap<>();
        List<SpecialClinic> list = specialClinicService.getSpecialClinicsByDepartmentId(departmentId);
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/{clinicId}")
    public Map<String, Object> getSpecialClinicById(@PathVariable Integer clinicId) {
        Map<String, Object> result = new HashMap<>();
        SpecialClinic specialClinic = specialClinicService.getSpecialClinicById(clinicId);
        result.put("code", 200);
        result.put("data", specialClinic);
        result.put("message", "查询成功");
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addSpecialClinic(@RequestBody SpecialClinic clinic) {
        Map<String, Object> result = new HashMap<>();
        int res = specialClinicService.insert(clinic);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "新增成功");
        } else {
            result.put("code", 500);
            result.put("message", "新增失败");
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> updateSpecialClinic(@RequestBody SpecialClinic clinic) {
        Map<String, Object> result = new HashMap<>();
        int res = specialClinicService.updateSpecialClinic(clinic);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "更新失败");
        }
        return result;
    }

    @DeleteMapping("/delete/{clinicId}")
    public Map<String, Object> deleteSpecialClinic(@PathVariable Integer clinicId) {
        Map<String, Object> result = new HashMap<>();
        int res = specialClinicService.deleteSpecialClinic(clinicId);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
}